﻿using System;
using System.Collections.Generic;
using System.Text;
using Common;
using Kingdee.BOS.Core.CloudModel;
using Kingdee.BOS.Core.CloudModel.Crm;
using Newtonsoft.Json;

namespace MethodSetBus
{
    public class Crm_Result
    {
        public IsSuccessResult Crm_SaveResult(string Type,string data,string _Materials)
        {
            IsSuccessResult isSuccess = new IsSuccessResult();
            string WriteType = "";
            try
            {
                switch (Type)
                {
                    case "Products":
                        WriteType = "物料";
                        Crm_MaterialResult<List<Crm_MaterialResultMsg>> Crm_Result = JsonConvert.DeserializeObject<Crm_MaterialResult<List<Crm_MaterialResultMsg>>>(data);
                        if (Crm_Result.msg[0].status == "success")
                        {
                            LogHelper.WriteLog(WriteType, Convert.ToInt32(Crm_Result.msg[0].record), Crm_Result.msg[0].product_no, 1, "金蝶写入灵当",_Materials, data, "", "");
                            NeedsResult needs = new NeedsResult();
                            needs.Id = Convert.ToInt32(Crm_Result.msg[0].record);
                            needs.Number = Crm_Result.msg[0].product_no;

                            isSuccess.Code = 0;
                            isSuccess.Result = needs;
                            isSuccess.Message = null;
                        }
                        else
                        {
                            //Crm_MaterialResult<string> Crm_ResultMsg = JsonConvert.DeserializeObject<Crm_MaterialResult<string>>(data);
                            LogHelper.WriteLog(WriteType, 0, "", 0, "金蝶写入灵当", _Materials, data, "", "");
                            isSuccess.Code = 500;
                            isSuccess.Result = null;
                            isSuccess.Message = Crm_Result.msg[0].msg;
                        }
                        break;
                    case "Accounts":
                        WriteType = "客户";
                        Crm_MaterialResult<List<Crm_AccountsMsg>> _AccountsMsg = JsonConvert.DeserializeObject<Crm_MaterialResult<List<Crm_AccountsMsg>>>(data);
                        if (string.IsNullOrEmpty(_AccountsMsg.msg[0].msg))
                        {
                            LogHelper.WriteLog(WriteType, Convert.ToInt32(_AccountsMsg.msg[0].record), _AccountsMsg.msg[0].vendor_no, 1, "金蝶写入灵当", _Materials, data, "", "");
                            NeedsResult needs = new NeedsResult();
                            needs.Id = Convert.ToInt32(_AccountsMsg.msg[0].record);
                            needs.Number = _AccountsMsg.msg[0].vendor_no;

                            isSuccess.Code = 0;
                            isSuccess.Result = needs;
                            isSuccess.Message = null;
                        }
                        else
                        {
                            LogHelper.WriteLog(WriteType, 0, "", 0, "金蝶写入灵当", _Materials, data, "", "");
                            isSuccess.Code = 500;
                            isSuccess.Result = null;
                            isSuccess.Message = _AccountsMsg.msg[0].msg;
                        }
                        break;
                    default:
                        isSuccess.Code = 500;
                        isSuccess.Result = null;
                        isSuccess.Message = "系统发生异常！";
                        break;
                }
                return isSuccess;
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(WriteType, 0, "", 0, "金蝶写入灵当", JsonConvert.SerializeObject(_Materials), "", e.Message, "");
                isSuccess.Code = 500;
                isSuccess.Result = null;
                isSuccess.Message = "系统发生异常！";
                return isSuccess;
            }
        }
    }
}
